﻿namespace Newtonsoft.Json.Utilities
{
    using System;
    using System.IO;
    using System.Web.UI;

    internal static class JavaScriptUtils
    {
        public static string GetCallbackEventFunction(Page page, Control control)
        {
            string str = page.ClientScript.GetCallbackEventReference(control, "eventArgument", "eventCallback", "context", "errorCallback", true);
            return ("function(eventArgument,eventCallback,context,errorCallback){" + str + "}");
        }

        public static string GetCallbackEventFunction(Page page, Control control, string argument)
        {
            string str = page.ClientScript.GetCallbackEventReference(control, "'" + argument + "'", "eventCallback", "context", "errorCallback", true);
            return ("function(eventCallback,context,errorCallback){" + str + "}");
        }

        public static string ToEscapedJavaScriptString(string value)
        {
            return ToEscapedJavaScriptString(value, '"', true);
        }

        public static string ToEscapedJavaScriptString(string value, char delimiter, bool appendDelimiters)
        {
            int? length = StringUtils.GetLength(value);
            using (StringWriter writer = StringUtils.CreateStringWriter(length.HasValue ? length.GetValueOrDefault() : 0x10))
            {
                WriteEscapedJavaScriptString(writer, value, delimiter, appendDelimiters);
                return writer.ToString();
            }
        }

        public static void WriteEscapedJavaScriptChar(TextWriter writer, char c, char delimiter)
        {
            switch (c)
            {
                case '\'':
                    writer.Write((delimiter == '\'') ? @"\'" : "'");
                    return;

                case '\\':
                    writer.Write(@"\\");
                    return;

                case '\b':
                    writer.Write(@"\b");
                    return;

                case '\t':
                    writer.Write(@"\t");
                    return;

                case '\n':
                    writer.Write(@"\n");
                    return;

                case '\f':
                    writer.Write(@"\f");
                    return;

                case '\r':
                    writer.Write(@"\r");
                    return;

                case '"':
                    writer.Write((delimiter == '"') ? "\\\"" : "\"");
                    return;
            }
            if (c > '\x001f')
            {
                writer.Write(c);
            }
            else
            {
                StringUtils.WriteCharAsUnicode(writer, c);
            }
        }

        public static void WriteEscapedJavaScriptString(TextWriter writer, string value, char delimiter, bool appendDelimiters)
        {
            if (appendDelimiters)
            {
                writer.Write(delimiter);
            }
            if (value != null)
            {
                for (int i = 0; i < value.Length; i++)
                {
                    WriteEscapedJavaScriptChar(writer, value[i], delimiter);
                }
            }
            if (appendDelimiters)
            {
                writer.Write(delimiter);
            }
        }
    }
}

